import { getItem, setItem } from '@/utils/storage'

const FA_KEY = 'MY_FAVORITE'

export const getFavoriteList = () => {
  return getItem(FA_KEY)
}

export const findFavoriteItem = (item) => {
  const { id } = item
  const data = getItem(FA_KEY) || []

  return !!data.find(e => e.id === id)
}

export const addFavoriteItem = (item) => {
  const data = getItem(FA_KEY) || []
  data.unshift(item)

  setItem(FA_KEY, data)
}

export const removeFavoriteItem = (item) => {
  const data = getItem(FA_KEY) || []
  const result = data.filter(book => {
    return book.id !== item.id
  })

  setItem(FA_KEY, result)
  return result
}
